package client

import (
	"context"
	"errors"
	"feng-che.com/server/driver-quiz/internal/constants"
	"feng-che.com/server/driver-quiz/internal/dep_db"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"luyouxin.com/person"
	"mongoDB"
	"time"
)

// Client /*
type Client struct {
	mongoDB.MongoDefaultModelNonID `bson:"basic"`
	ID                             primitive.ObjectID `bson:"_id,omitempty" json:"id"`
	person.Person
	SchoolID         primitive.ObjectID `bson:"school_id" json:"schoolId"`                  // 所属学校
	CoachID          primitive.ObjectID `bson:"coach_id" json:"coachId"`                    // 所属教练
	FrontDeskID      primitive.ObjectID `bson:"front_desk_id" json:"frontDeskId"`           // 所属前台
	PrincipalID      primitive.ObjectID `bson:"principal_id" json:"principalId"`            // 所属前台
	RookieOfSubject2 bool               `bson:"rookie_of_subject2" json:"rookieOfSubject2"` //  rookie of subject2 是否有过科目2的学习 -- 判断加练
	RookieOfSubject3 bool               `bson:"rookie_of_subject3" json:"rookieOfSubject3"` //  rookie of subject3 是否有过科目3的学习
	// 以下是绑定的车辆信息
	VehicleModel string             `bson:"vehicle_model" json:"vehicleModel"` // 车辆型号
	ExamType     constants.ExamType `bson:"exam_type" json:"examType"`         // 考试类型 c1 c2
	/// 以下是文字回复内容
	SchoolName string `bson:"-" json:"schoolName"`
}

func (c *Client) Save(ctx context.Context, updateOptions ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
	c.UpdatedAt = time.Now()
	if c.ID.IsZero() {
		c.ID = primitive.NewObjectID()
	}
	if len(updateOptions) == 0 {
		updateOptions = append(updateOptions, options.Update().SetUpsert(true))
	}
	result, err := c.GetCollection().UpdateByID(
		ctx,
		c.ID,
		bson.D{
			{"$set", c},
		},
		updateOptions...,
	)
	return result, err
}

func (c *Client) Update(ctx context.Context, dataFields *bson.D, options ...*options.UpdateOptions) error {
	if c.ID.IsZero() {
		return errors.New("update client fail: ID is required")
	}
	if dataFields == nil {
		return errors.New("update client fail: dataFields is required")
	}
	*dataFields = append(*dataFields, bson.E{Key: "$set", Value: bson.D{
		{"basic.updated_at", time.Now()},
	}})
	_, err := c.GetCollection().UpdateByID(ctx, c.ID, *dataFields, options...)
	return err
}

func (c *Client) Delete(ctx context.Context) error {
	c.DeletedAt = time.Now()
	c.Deleted = true
	return c.Update(ctx, &bson.D{
		{"$set", bson.D{
			{"basic.deleted_at", c.DeletedAt},
			{"basic.deleted", c.Deleted},
		}},
	})
}

func (c *Client) GetCollectionName() string {
	return "client"
}

func (c *Client) GetCollection() *mongo.Collection {
	name := c.GetCollectionName()
	return dep_db.ObjectCollection(&name)
}

// CanStudentLineUp 学员排队资格检查
func (c *Client) CanStudentLineUp() (reason string, yes bool) {
	if c.Deleted {
		return "学员被删除了", false
	}
	return "", true
}

// CanBeSchoolBind 学员是否可以被绑定
func (c *Client) CanBeSchoolBind() (yes bool) {
	if c.Deleted {
		return false
	}
	if !c.SchoolID.IsZero() {
		return false
	}
	return true
}
